var User = require('../../model/user');

exports.admin = function(req, res) {
    res.render('admin/index');
}

exports.profile=function(req,res){

    res.render('admin/profile')
}


/**
 *查询所有用户
 */
exports.fetch = function(req, res) {
    User.fetch(function(err, users) {
        if (err) {
            console.log(err);
        } else {
            res.render('admin/user', {
                users: users
            });
        }
    });
}

/**
 * 登出
 */
exports.logout = function(req, res) {
    delete req.session.user
    res.redirect('/')
}

/**
 * 判断用户是否登陆
 */
exports.signinRequired = function(req, res, next) {
    var _user = req.session.user

    if (!_user) {
        return res.redirect('/signin');
    }
    next();

}

/**
 * 判断用户是否为管理员
 */
exports.adminRequired = function(req, res, next) {
    var _user = req.session.user
    console.log(_user)
    if (_user.role != 1) {
        return res.redirect('/signin')
    }
    next()

}
